(function($) {
	// 快速登录弹窗时的ajax接口
	var popRegisterAPI = '/portal/quickRegister.jspa';
	var checkLogNameAPI = ':';
	// ----------------------------
	YM.namespace('YM.page.register');
	// ----------------------------
	YM.page.register.init = function() {
		this.initElemEvents();
		this.initFormEvents();
		this.setPopElementId();
		if ($('.hd-crossdomain').val()=='true') {
			YM.page.setDomain();
		}
	};
	YM.page.register.initElemEvents = function() {
		$('iput.txt-user').bindYMUI('InputBGHint', 'hint-user');
		$('input.txt-pass').bindYMUI('InputBGHint', 'hint-pass');
		$('input.txt-passcode').bindYMUI('InputBGHint', 'hint-passcode');
		$('input.txt-recommend').bindYMUI('InputBGHint', 'hint-recommend');
		$('input.txt-user').attr('maxlength',50);
		$('input.txt-user').bindYMUI('validFormElement', {
			validEmpty:true, emptyError:'请输入E-mail或手机号',
			validMethod:'log-name', methodError:'请输入正确的E-mail或手机号',
			validAjax:function(element, d) {
				return (d.flag)?'':d.msg;
			}
		});
		$('input.txt-pass').bindYMUI('validFormElement', {
			validEmpty:true, emptyError:'请输入密码',
			validMethod:'log-pass', methodError:'密码请设为6-16位字母或数字'
		});
		$('input.txt-pass2').bindYMUI('validFormElement', {
			validEmpty:true, emptyError:'请输入确认密码',
			validCompare:$('input.txt-pass'), compareError:'两次密码输入不一致'
		});
		$('input.txt-passcode').bindYMUI('validFormElement', {
			validEmpty:true, emptyError:'请输入PASS码',
			premise: function() {
				return $('input.chk-vip').attr('checked');
			}
		});
		$('input.txt-randcode').bindYMUI('validFormElement', {
			validEmpty:true, emptyError:'请输入验证码',
			premise: function() {
				return $('input.chk-vip').attr('checked');
			}
		});
		$('a.btn-change-randimg').click(function() {
			$(this).prev().trigger('changeImage');
		});
		$('input.chk-vip').click(function() {
			$('#vipinfo')[this.checked?'show':'hide']();			
			if (parent.YM && parent.YM.util && parent.YM.util.dialog) {
				parent.YM.util.dialog.setWidth(this.checked?480:400);
				parent.YM.util.dialog.setHeight(this.checked?520:400);
			}
		});
		$('input.chk-clause').click(function() {
			if (this.checked) $(this).parent().bindYMUI('ShowErrorString');
		});
		if ($('input.chk-vip').attr('checked')) $('#vipinfo').show();
		$('img.rand-img').bind('changeImage', function() {
			this.src = '/random/rand.jspa?d='+ new Date().getTime();
		});
		$('button.btn-register').attr('disabled', false);
		$('button.btn-register').bind('disableStatus', function() {
			$(this).attr('disabled', true);
			$(this).addClass('disabled');
			$(this).data('oldvalue', $(this).html());
			$(this).html('提交注册中');
		});
		$('button.btn-register').bind('enableStatus', function() {
			$(this).attr('disabled', false);
			$(this).removeClass('disabled');
			$(this).html($(this).data('oldvalue'));
		});
	};
	YM.page.register.initFormEvents = function() {
		$('form.frm-register').bind('submitAjaxRegister', function() {
			$.ajax({ url:popRegisterAPI, data:$(this).serialize(), dataType:'json', type:'post', success:function(data) {
				if (data.flag == 1) {
					YM.page.register.ajaxLoginSuccess();
				} else {
					var msg = data.msg.split('~');
					if (msg[0]!='quick-register-confirmClause') {
						$('#'+ msg[0]).bindYMUI('ShowErrorString', msg[1]);
						$('#'+ msg[0]).data('error', false);
					} else {
						$('#'+ msg[0]).parent().bindYMUI('ShowErrorString', msg[1]);
					}
					$('.btn-register').trigger('enableStatus');
				}
			}});
		});
		$('form.frm-register').submit(function() {
			if (YM.data.checkElementsInvalid('input.txt-user, input.txt-pass, input.txt-pass2')) return false;
			if ($('input.chk-vip').attr('checked')) {
				if (YM.data.checkElementsInvalid('input.txt-passcode, input.txt-randcode')) return false;
			}
			if (!$('input.chk-clause').attr('checked')) {
				$('input.chk-clause').parent().bindYMUI('ShowErrorString', '请接受Yesmywine服务条款');
				return false;
			}
			if ($('.hd-pagetype').val()=='pop') {
				$('button.btn-register').trigger('disableStatus');
				$(this).trigger('submitAjaxRegister');
				return false;
			}
			$('.btn-register').trigger('disableStatus');
			return true;
		});
	};
	YM.page.register.setPopElementId = function() {
		if ($('.hd-pagetype').val()=='pop') {
			$('input.txt-user').attr('id', 'quick-register-username');
			$('input.txt-pass').attr('id', 'quick-register-pwd');
			$('input.txt-pass2').attr('id', 'quick-register-rePwd');
			$('input.chk-vip').attr('id', 'quick-register-to-be-vip');
			$('input.txt-passcode').attr('id', 'quick-register-passCode');
			$('input.txt-randcode').attr('id', 'quick-register-identifyCode');
			$('input.chk-clause').attr('id', 'quick-register-confirmClause');
		}
	};
	YM.page.register.ajaxLoginSuccess = function() {
		var to = $('input.hd-to').val();
		if (to != '') {
			document.location.href = to;
		} else if (parent.YM && parent.YM.login && parent.YM.login.status) {
			parent.YM.login.success(true);
		}
	};
	$(document).ready(function() {
		YM.page.register.init();		
	});
})(jQuery);